<?php
/**
 * A Doctrine 1.2 Model class to work with PiePHP.
 *
 * @author     Bill Lee <bill@cachecrew.com>
 * @package    PiePHP
 * @since      Version 0.0
 * @copyright  Copyright (c) 2011, Pie Software Foundation
 * @license    http://www.piephp.com/license
 *
 * @see http://www.doctrine-project.org/projects/orm/1.2/api
 */

/**
 * Implement model that implements Doctrine 1.2 consistent with PiePHP model
 * usage semantics. For more robust Doctrine usage, you can adjust your 
 * controller to use the Doctrine model in the normal Doctrine expected manner.
 * 
 * Usage example:
 *   In index.php:
 *     spl_autoload_register('__autoload'); // If you have an __autoload
 *											// If '/Doctrine' not in PiePHP root:
 * 	   DoctrineDatabase::setDoctrinePath('/path/to/Doctrine'); 
 *   In your Controller:
 *     $this->defaultModelClassName = 'DoctrineModel';
 *	   $this->model = $this->loadModel();
 *     $res = $this->model->select('* from table');
 *
 * @see DoctrineDatabase
 */
class DoctrineModel extends Model {

	public function loadDatabase( $databaseConfigName = NULL) {
		global $DATABASES;
		if ($databaseConfigName) {
			$this->databaseConfigName = $databaseConfigName;
		}
		if (!$this->database && $this->databaseConfigName) {
			$database = $DATABASES[$this->databaseConfigName];
			
			if (is_string($database)) {
				list($type, $config) = $this->getTypeAndConfig($database);
				$config['type'] = $type;
				$this->database = new DoctrineDatabase($config, $this->databaseConfigName);
				
				$DATABASES[$this->databaseConfigName] = $this->database;
			}
			else {
				$this->database = $database;
			}

		}
		return $this->database;

		
	} // function loadDatabase()

} // class DoctrineModel